/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-07-30
 * Time: 8:54
 */

public class Solution {
    public int compress(char[] chars) {
        char c = chars[0];
        int before = 0;
        int index = 1; // 记录将要被更新的位置
        int len = chars.length;
        for (int i = 0;i <= len;i++) {
            if (i == len || chars[i] != c) {
                // 记录长度
                int num = i - before;
                if (num != 1) {
                    // 不只一个, 将后面的数字加上去
                    String s = String.valueOf(num);
                    int length = s.length();
                    // 更新数组
                    for (int j = 0;j < length;j++) {
                        chars[index+j] = s.charAt(j);
                    }
                    // 更新位置
                    index += length;
                }
                if (i < len) {
                    // 更新字符
                    c = chars[i];
                    // 更新首字符的位置
                    before = i;
                    // 将新的字符填进去
                    chars[index] = c;
                    index++;
                }
            }
        }
        return index;
    }
}



